home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 8 / Revista CD Expert nº 08 CD1.iso / Utilitarios / Programacao / MS-DOS Interrupt List / inter60g / INT2IPF.ZIP / int2ipf.c next >
C/C++ Source or Header  |  1997-04-06  |  14KB  |  404 lines

  1. /*
  2.         ┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
  3.         ├─┼─┼─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼─┼─┤
  4.         ├─┼─┤   PROJECT      : INT2IPF - Convert all files to .IPF    ├─┼─┤
  5.         ├─┼─┤   FILE         : INT2IPF.C - Main Module                ├─┼─┤
  6.         ├─┼─┤   Last modified: 20 Feb 97                              ├─┼─┤
  7.         ├─┼─┼─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┼─┼─┤
  8.         └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘
  9.  
  10.     INT2IPF - Convert INTERRUP.LST to .IPF format for the OS/2 IPF Compiler
  11.  
  12.     Processes the output of COMBINE.COM (interrup.lst)
  13.     INT2IPF interrup.lst interrup.ipf
  14.     Then use IPFC INTERRUP.IFP /INF
  15.     IE:
  16.  
  17.     COMBINE
  18.     INT2IPF interrup.lst interrup.ipf
  19.     IPFC interrup.ipf /INF
  20.  
  21. WarpSpeed Computers - The Graham Utilities for OS/2.
  22. Voice:  +61-3-9384-1060  PO Box 212   FidoNet:     3:632/344
  23. FAX:    +61-3-9386-9979  Brunswick    Internet:    chrisg@warpspeed.com.au
  24. BBS:    +61-3-9386-3104  VIC 3056     CompuServe:  100250,1645
  25. 300-28,800  N,8,1 ANSI   Australia    Web Pages:
  26.                                       http://www.netins.net/showcase/spectre
  27.                                       http://www.warpspeed.com.au
  28.  
  29. */
  30. #define         INT2IPF
  31.  
  32. /* ------------------------------------------------------------------------ */
  33. /* History                                                                  */
  34. /* ------------------------------------------------------------------------ */
  35. /*
  36.     1.00    08/02/97    Initial Version
  37.     1.01    20/02/97    Added in index support. Due to duplicates the
  38.                         :i2. tags have been commented out.
  39. */
  40. /* ------------------------------------------------------------------------ */
  41. /* Include files                                                            */
  42. /* ------------------------------------------------------------------------ */
  43.  
  44. #define         INCL_DOS
  45. #define         INCL_NOPM
  46. #define         INCL_KBD
  47. #include        <os2.h>
  48.  
  49. #include        <stdio.h>
  50. #include        <malloc.h>
  51. #include        <stdlib.h>
  52. #include        <ctype.h>
  53. #include        <stdarg.h>
  54. #include        <string.h>
  55.  
  56. #include        "warpcomm.h"
  57.  
  58. /* ------------------------------------------------------------------------ */
  59. /* Definitions                                                              */
  60. /* ------------------------------------------------------------------------ */
  61.  
  62. #define     Version "[INT2IPF, V1.01 - 20/02/97 - (C) Chris Graham - WarpSpeed Computers]\n"
  63.  
  64. /* ------------------------------------------------------------------------ */
  65. /* External references                                                      */
  66. /* ------------------------------------------------------------------------ */
  67.  
  68. /* ------------------------------------------------------------------------ */
  69. /* Foreward references                                                      */
  70. /* ------------------------------------------------------------------------ */
  71.  
  72. BOOL                Convert_File( PSZ pszFileIn, PSZ pszFileOut ) ;
  73. void                Parse_CmdLine( int *argc, char *argv[] ) ;
  74. void                Usage( void ) ;
  75. void                DoHeader( FILE * fpFileOut ) ;
  76. void                DoFooter( FILE * fpFileOut ) ;
  77. void                Replace_String( PSZ pszSource, PSZ pszSearch, PSZ pszReplace ) ;
  78. void                ParseLine( PSZ pszLine ) ;
  79. void                DoSeparator( FILE *fpFileOut, PSZ pszLine, ULONG ulCount ) ;
  80.  
  81. /* ------------------------------------------------------------------------ */
  82. /* Constant Local Data                                                      */
  83. /* ------------------------------------------------------------------------ */
  84.  
  85. /* ------------------------------------------------------------------------ */
  86. /* Global Data                                                              */
  87. /* ------------------------------------------------------------------------ */
  88.  
  89. /* ------------------------------------------------------------------------ */
  90. /* Code                                                                     */
  91. /* ------------------------------------------------------------------------ */
  92.  
  93. int                 main( int argc, char *argv[] )
  94. {
  95.     setvbuf( stdout, NULL, _IONBF, 0 ) ;
  96.  
  97.     printf( Version ) ;
  98.  
  99.     Parse_CmdLine( &argc, argv ) ;
  100.  
  101.     if ( 3 == argc )
  102.         {
  103.         if ( !Convert_File( argv[1], argv[2] ) )
  104.             {
  105.             Usage() ;
  106.             }
  107.         }
  108.     else
  109.         {
  110.         Usage() ;
  111.         exit( 1 ) ;
  112.         }
  113.  
  114.     return( 0 ) ;
  115. }
  116.  
  117. BOOL                Convert_File( PSZ pszFileIn, PSZ pszFileOut )
  118. {
  119. FILE                *fpFileIn, *fpFileOut ;
  120. PSZ                 pszLine ;
  121. ULONG               ulCount ;
  122.  
  123.     fpFileIn = fopen( pszFileIn, READ_TEXT ) ;
  124.     if ( NULL == fpFileIn )
  125.         {
  126.         perror( pszFileIn ) ;
  127.         return( FALSE ) ;
  128.         }
  129.     else
  130.         {
  131.         printf( "Processing %s\n", pszFileIn ) ;
  132.         }
  133.  
  134.     fpFileOut = fopen( pszFileOut, WRITE_BINARY ) ;
  135.     if ( NULL == fpFileOut )
  136.         {
  137.         perror( pszFileOut ) ;
  138.         return( FALSE ) ;
  139.         }
  140.     else
  141.         {
  142.         printf( "Producing %s\n", pszFileOut ) ;
  143.         }
  144.  
  145.     ulCount = 0L ;
  146.  
  147.     DoHeader( fpFileOut ) ;
  148.  
  149.     pszLine = (PSZ) malloc( (size_t) 32768 ) ;
  150.     if ( NULL != pszLine )
  151.         {
  152.         while ( NULL != fgets( pszLine, (int) 32767, fpFileIn ) )
  153.             {
  154.             if ( 0 == strncmp( pszLine, "--------!---Section", 19 ) )
  155.                 {
  156.                 /* Remove the multiple section lines */
  157.                 fgets( pszLine, (int) 32767, fpFileIn ) ;
  158.                 fgets( pszLine, (int) 32767, fpFileIn ) ;
  159.                 ulCount = ulCount + 3 ;
  160.                 }
  161.             else
  162.                 {
  163.                 /* Otherwise, process normal lines */
  164.                 ulCount++ ;
  165.                 ParseLine( pszLine ) ;
  166.                 if ( 0 == strncmp( pszLine, "--------", 8 ) )
  167.                     {
  168.                     /* Do the divider line */
  169.                     DoSeparator( fpFileOut, pszLine, ulCount ) ;
  170.                     }
  171.                 else
  172.                     {
  173.                     /* Do other lines */
  174.                     fputs( pszLine, fpFileOut ) ;
  175.                     }
  176.                 }
  177.             }
  178.         free( pszLine ) ;
  179.         }
  180.  
  181.     DoFooter( fpFileOut ) ;
  182.  
  183.     fclose( fpFileOut ) ;
  184.     fclose( fpFileIn ) ;
  185.  
  186.     printf( "%lu lines processed.\n", ulCount + 1 ) ;
  187.  
  188.     return( TRUE ) ;
  189. }
  190.  
  191. void                Parse_CmdLine( int *argc, char *argv[] )
  192. {
  193. int                 i, j ;
  194.  
  195.     /* Parse for command line switches */
  196.     for ( i = 1; argv[i]; i++ )
  197.         {
  198.         if ( '/' == *argv[i] || '-' == *argv[i] )
  199.             {
  200.             /* Accept slashes or dashes */
  201.             for ( j = 1; argv[i][j]; j++ )
  202.                 {
  203.                 switch (tolower(argv[i][j]))
  204.                     {
  205.                     case '?':
  206.                     case '*':
  207.                         Usage() ;
  208.                         exit(1) ;
  209.                         break ;
  210.                     default:
  211.                         {
  212.                         printf( "Unknown option \"%s\"\n", argv[i] ) ;
  213.                         Usage() ;
  214.                         exit(1) ;
  215.                         }
  216.                     }
  217.                 }
  218.             for ( j = i; j < *argc; j++ )
  219.                 {
  220.                 /* remove that argv from the list */
  221.                 argv[j] = argv[j + 1] ;
  222.                 }
  223.             argv[*argc] = NULL ;
  224.             if ( i != *argc )
  225.                 {
  226.                 (*argc)-- ;
  227.                 }
  228.             i-- ;
  229.             }
  230.         }
  231. }
  232.  
  233. void                Usage( void )
  234. {
  235.     printf( "Usage: INT2IPF <File In> <File Out>\n" ) ;
  236. }
  237.  
  238. void                DoHeader( FILE * fpFileOut )
  239. {
  240.     fprintf( fpFileOut, ".*****************************************************************************\n" ) ;
  241.     fprintf( fpFileOut, ".* Ralf Browns' Interrupt List                                               *\n" ) ;
  242.     fprintf( fpFileOut, ".*****************************************************************************\n" ) ;
  243.     fprintf( fpFileOut, ":userdoc.\n" ) ;
  244.     fprintf( fpFileOut, ":docprof toc=123.\n" ) ;
  245.     fprintf( fpFileOut, ":title.Ralf Browns' Interrupt List\n" ) ;
  246.     fprintf( fpFileOut, ":body.\n" ) ;
  247.     fprintf( fpFileOut, ":h1.\n" ) ;
  248.     fprintf( fpFileOut, "Ralf Browns' Interrupt List\n" ) ;
  249.     fprintf( fpFileOut, ":xmp.\n" ) ;
  250. }
  251.  
  252. void                DoFooter( FILE * fpFileOut )
  253. {
  254.     fprintf( fpFileOut, ":exmp.\n" ) ;
  255.     fprintf( fpFileOut, ".*****************************************************************************\n" ) ;
  256.     fprintf( fpFileOut, ":index.\n" ) ;
  257.     fprintf( fpFileOut, ":euserdoc.\n" ) ;
  258. }
  259.  
  260. void                Replace_String( PSZ pszSource, PSZ pszSearch, PSZ pszReplace )
  261. {
  262. PCHAR               pC1, pC2, pC3, pC4 ;
  263. int                 i ;
  264.  
  265.     i = strlen( pszSearch ) ;
  266.     pC3 = pC2 = malloc( (size_t) 32768 ) ;
  267.     pC1 = pszSource ;
  268.     while ( '\0' != *pC1 )
  269.         {
  270.         if ( 0 == strnicmp( pC1, pszSearch, i ) )
  271.             {
  272.             pC4 = pszReplace ;
  273.             while ( *pC4 )
  274.                 {
  275.                 *pC2++ = *pC4++ ;
  276.                 }
  277.             pC1++ ;
  278.             }
  279.         else
  280.             {
  281.             *pC2++ = *pC1++ ;
  282.             }
  283.         }
  284.     *pC2 = '\0' ;
  285.     strcpy( pszSource, pC3 ) ;
  286.     free( pC3 ) ;
  287. }
  288.  
  289. void                ParseLine( PSZ pszLine )
  290. {
  291.     /* TODO: Smart Tab Stops */
  292.     Replace_String( pszLine, "\t", "        " ) ;
  293.     Replace_String( pszLine, "&",  "&."    ) ;
  294.     Replace_String( pszLine, ":",  "&colon."  ) ;
  295. }
  296.  
  297. void                DoSeparator( FILE *fpFileOut, PSZ pszLine, ULONG ulCount )
  298. {
  299. static char         szINT[]        = "INT xx" ;
  300. static char         szAX[]         = "AX = xxxx" ;
  301. static char         szAH[]         = "AH = xx" ;
  302. static char         szCurrentInt[] = "  " ;
  303. static char         szInterrupt[]  = "Interrupt xxh" ;
  304. static ULONG        ulRefID ;
  305. char                szBuffer[100] ;
  306. PSZ                 pC1, pC2 ;
  307. int                 nLevel ;
  308.  
  309.     fprintf( fpFileOut, ":exmp.\n" ) ;
  310.     szINT[4] = pszLine[10] ;
  311.     szINT[5] = pszLine[11] ;
  312.     if ( 0 == strcmp( szCurrentInt, "--" ) )
  313.         {
  314.         nLevel = 1 ;
  315.         }
  316.     else
  317.         {
  318.         if ( szINT[4] == szCurrentInt[0] && szINT[5] == szCurrentInt[1] )
  319.             {
  320.             /* Same level */
  321.             nLevel = 2 ;
  322.             }
  323.         else
  324.             {
  325.             /* Different level, reset to primary level */
  326.             szInterrupt[10] = pszLine[10] ;
  327.             szInterrupt[11] = pszLine[11] ;
  328.             if ( '-' != pszLine[10] && '-' != pszLine[11] )
  329.                 {
  330.                 fprintf( fpFileOut, ":h1 id=%lu.%s\n", ulCount - 1, szInterrupt ) ;
  331.                 fprintf( fpFileOut, ":i1 id=%lu.%s\n", ulCount - 1, szInterrupt ) ;
  332.                 ulRefID = ulCount - 1 ;
  333.                 /* IPFC whinges if there is no text between :h tags, so we give it some */
  334.                 fprintf( fpFileOut, "%s\n", szInterrupt ) ;
  335.                 }
  336.             nLevel = 2 ;
  337.             }
  338.         }
  339.  
  340.     if ( '-' == pszLine[10] && '-' == pszLine[11] )
  341.         {
  342.         /* This covers the headers (no interrupt) */
  343.         pC1 = &pszLine[12] ;
  344.         pC2 = szBuffer ;
  345.         while ( '\0' != *pC1 )
  346.             {
  347.             if ( '-' != *pC1 )
  348.                 {
  349.                 /* Copy non '-' chars */
  350.                 *pC2++ = *pC1++ ;
  351.                 }
  352.             else
  353.                 {
  354.                 /* skip '-' chars */
  355.                 pC1++ ;
  356.                 }
  357.             }
  358.         *pC2 = '\0' ;
  359.         fprintf( fpFileOut, ":h1 id=%lu.%s\n", ulCount, szBuffer ) ;
  360.         fprintf( fpFileOut, ":i1 id=%lu.%s\n", ulCount, szBuffer ) ;
  361.         ulRefID = ulCount ;
  362.         }
  363.     else
  364.         {
  365.         if ( '-' == pszLine[14] && '-' == pszLine[15] )
  366.             {
  367.             szAH[5] = pszLine[12] ;
  368.             szAH[6] = pszLine[13] ;
  369.             if ( '-' != pszLine[12] && '-' != pszLine[13] )
  370.                 {
  371.                 fprintf( fpFileOut, ":h%d id=%lu.%s %s\n", nLevel, ulCount, szINT, szAH ) ;
  372.                 /*
  373.                 fprintf( fpFileOut, ":i2 refid=%lu.%s %s\n", ulRefID, szINT, szAH ) ;
  374.                 */
  375.                 }
  376.             else
  377.                 {
  378.                 fprintf( fpFileOut, ":h%d id=%lu.%s\n", nLevel, ulCount, szINT ) ;
  379.                 /*
  380.                 fprintf( fpFileOut, ":i2 refid=%lu.%s\n", ulRefID, szINT ) ;
  381.                 */
  382.                 }
  383.             }
  384.         else
  385.             {
  386.             szAX[5] = pszLine[12] ;
  387.             szAX[6] = pszLine[13] ;
  388.             szAX[7] = pszLine[14] ;
  389.             szAX[8] = pszLine[15] ;
  390.             fprintf( fpFileOut, ":h%d id=%lu.%s %s\n", nLevel, ulCount, szINT, szAX ) ;
  391.             /*
  392.             fprintf( fpFileOut, ":i2 refid=%lu.%s %s\n", ulRefID, szINT, szAX ) ;
  393.             */
  394.             }
  395.         }
  396.  
  397.     if ( '-' != pszLine[10] && '-' != pszLine[11] )
  398.         {
  399.         szCurrentInt[0] = pszLine[10] ;
  400.         szCurrentInt[1] = pszLine[11] ;
  401.         }
  402.     fprintf( fpFileOut, ":xmp.\n" ) ;
  403. }
  404.